package com.ssyc.student.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UIHelper;
import com.ssyc.student.R;
import com.ssyc.student.activity.StudentWriteActivity;
import com.ssyc.student.bean.WriteInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 学生端--写作
 */

public class StudentWriteAnswerFragment extends LazyBaseFragment implements View.OnClickListener{

    //通知Acitvity进行ViewPager翻页的标识
    public static String WRITEVPSCOLL = "StudentWriteActivityVpScoll";

    //题目集合
    public List<WriteInfo.DataBean.QuestionListBean.DetailListBean> datas;

    //当前题目位置
    private int currPos;

    //小标题
    private TextView tv_title;

    //按钮组
    private RelativeLayout rl_bts;

    //清除按钮
    private TextView tv_clean;

    //确定
    private TextView tv_sure;

    //答案部分
    private LinearLayout ll_answer;

    //我的作答
    private TextView tv_my_answer;

    //真实答案
    private TextView tv_real_answer;

    //真实答案
    private EditText et_text;
    /**
     * 提供的生成当前Fragment的方法
     *
     * @param data 包含页面数据的json字符串
     * @return
     */
    public static StudentWriteAnswerFragment newInstance(String data, int pos) {

        StudentWriteAnswerFragment StudentWriteAnswerFragment = new StudentWriteAnswerFragment();
        Bundle bundle = new Bundle();
        bundle.putString("data", data);
        bundle.putInt("pos", pos);
        StudentWriteAnswerFragment.setArguments(bundle);

        return StudentWriteAnswerFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_student_write_edit_answer;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {
        //初始化视图
        initView(view);

        showContent();

        //模拟提供答案选项的数据
        initData();
    }


    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {
        //题目小标题
        tv_title = view.findViewById(R.id.tv_title);
        //按钮
        rl_bts = view.findViewById(R.id.rl_bts);
        //清除按钮
        tv_clean = view.findViewById(R.id.tv_clean);
        //确定
        tv_sure = view.findViewById(R.id.tv_sure);
        //答案部分
        ll_answer = view.findViewById(R.id.ll_answer);
        //我的作答
        tv_my_answer = view.findViewById(R.id.tv_my_answer);
        //真实答案
        tv_real_answer = view.findViewById(R.id.tv_real_answer);

        //输入框
        et_text = view.findViewById(R.id.et_text);

        tv_clean.setOnClickListener(this);
        tv_sure.setOnClickListener(this);
    }

    /**
     * 模拟的答案选项
     */
    private void initData() {
        //获取当前页面的位置
        currPos = getArguments().getInt("pos");
        //获取传递过来的页面数据
        String dataJson = getArguments().getString("data");
        //解析Json获取数据
        datas = new ArrayList<>();
        datas = GsonUtil.jsonToBean(dataJson, WriteInfo.DataBean.QuestionListBean.class).getDetail_list();
        tv_title.setText(datas.get(currPos).getReference());
        tv_real_answer.setText(datas.get(currPos).getAnswer());
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.tv_clean) {
            //清除
            et_text.setText("");
        }else if(id == R.id.tv_sure){
            //确定
            if("".equals(et_text.getText().toString())){
                UIHelper.showToast(getActivity(),"内容不能为空");
                return;
            }
            //显示答案
            ll_answer.setVisibility(View.VISIBLE);
            rl_bts.setVisibility(View.GONE);
            et_text.setVisibility(View.GONE);
            tv_my_answer.setText(et_text.getText().toString());

            //将选择的答案保存到Acitivity的Map中
            StudentWriteActivity.getChooseAnswerMap().put(currPos,et_text.getText().toString());
        }
    }
}
